
package models.spider.stats;

import java.util.Date;

public class Stopwatch {

    protected String name  = null;
    protected Date   start = null;
    protected Date   end   = null;

    public Stopwatch(final String name) {
        start = new Date();
        this.name = name;
    }

    public Stopwatch() {
        this("");
    }

    public String getName() {
        return name;
    }

    public Stopwatch start() {
        start = new Date();
        return this;
    }

    public Stopwatch stop() {
        end = new Date();
        return this;
    }

    public long elapsed() {
        if (end == null) {
            return 0;
        }
        return (end.getTime() - start.getTime());
    }

    @Override
    public String toString() {
        return name + " elapsed = " + elapsed();
    }
}
